package com.coustemview.qianfeng.huaqiangbeistore.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.coustemview.qianfeng.huaqiangbeistore.R;
import com.coustemview.qianfeng.huaqiangbeistore.application.Myapplication;
import com.coustemview.qianfeng.huaqiangbeistore.config.Netconfig;
import com.coustemview.qianfeng.huaqiangbeistore.model.Oversear_item_Bean;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

public class Mainpage_oversear_shopActivity extends AppCompatActivity {
  private ImageView oversear_item_back,oversear_item_share,image_oversearitem_picture;
    private TextView text_voersearitem_sendtime,text_oversearitem_explain,text_oversearitem_price;
    private TextView text_oversearitem_oldprice,text_voersearitem_spec,text_oversearitem_recommendcontent;
    private ImageView image_voersearitem_shoppicture1,image_voersearitem_shoppicture2,image_voersearitem_shoppicture3,image_voersearitem_shoppicture4,image_voersearitem_shoppicture5;
    private ImageView image_oversearitem_ensure;
    private ImageView image_voersearitem_arrow;
    private PopupWindow popupWindow;
    private View view;
   private TextView oversear_itemspecexplain1,oversear_itemprice1,oversear_itemspecexplain2,oversear_itemprice2,oversear_itemspecexplain3,oversear_itemprice3,oversear_itemspecexplain4,oversear_itemprice4,oversear_itemspecexplain5,oversear_itemprice5,oversear_itemspecexplain6,oversear_itemprice6;
    private ImageView image_oversearitem_shopdetails,image_oversearitem_specnumber,image_oversearitem_eavluatedetails;
    private Myapplication application;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainpage_oversear_shop);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        oversear_item_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

     //分享
     oversear_item_share.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {

         }
     });

        //选择规格
        image_voersearitem_arrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPopupWindow();
            }
        });

    }

    private void setData() {
        image_oversearitem_shopdetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Intent intent=new Intent(getApplicationContext(),Oversear_ShopdetailsActivity.class);
                startActivity(intent);
            }
        });
        image_oversearitem_specnumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),Oversear_ShopdetailsActivity.class);
                startActivity(intent);
            }
        });

        image_oversearitem_eavluatedetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),Oversear_ShopdetailsActivity.class);
                startActivity(intent);
            }
        });
    }
   //选择规格用popupwindow做
    //创建popupwindow
    private void Initpopupwindow(){

        popupWindow = new PopupWindow(view,300,300);
        // 可获得焦点
        popupWindow.setFocusable(true);

        // anchor：PopupWindow挂载的控件
        // xoff,yoff:x轴与y州的偏移量
        popupWindow.showAsDropDown( image_voersearitem_arrow, 0, 0);

      WindowManager.LayoutParams params=  getWindow().getAttributes();
         params.alpha=0.7f;
        getWindow().setAttributes(params);


        //点击其他地方消失
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
               closePopupWindow();
                return false;
            }
        });
    }


    //获取popupwindow的实例
    private void getPopupWindow() {

        if (null != popupWindow) {
            closePopupWindow();
            return;
        } else {
            Initpopupwindow();
        }
    }

    //关闭
    private void closePopupWindow()
    {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
            popupWindow = null;
            WindowManager.LayoutParams params=getWindow().getAttributes();
            params.alpha=1f;
            getWindow().setAttributes(params);
        }
    }

    //popupwindow位置


    private void initData() {
        //初始化application
        application = (Myapplication) getApplication();


        Intent intent = getIntent();
         String skuid = intent.getStringExtra("skuid");
          application.setSkuid(skuid);
        int parseInt = Integer.parseInt(skuid);
        String price = intent.getStringExtra("price");
        String oldprice = intent.getStringExtra("oldprice");
          text_oversearitem_price.setText(price);
          text_oversearitem_oldprice.setText(oldprice);
        String path = String.format(Netconfig.OVERSEAR_ITEM_PATH, parseInt);
        RequestParams params=new RequestParams(path);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
            if(result!=null) {
                Gson mGson=new Gson();
                Oversear_item_Bean itemBean = mGson.fromJson(result, Oversear_item_Bean.class);
                Oversear_item_Bean.DataBean data = itemBean.getData();
                  int goodsId = data.getGoodsId();
                 application.setGoodsid(goodsId);
                String imag = data.getImag();
                  if(imag!=null) {
                      Picasso.with(getApplicationContext()).load(imag).into(image_oversearitem_picture);
                  }
                  String info = data.getSkuSupplyTypeInfo();
                    if(info!=null) {
                        text_voersearitem_sendtime.setText(info);
                    }
                   String title = data.getTitle();
                  text_oversearitem_explain.setText(title);

                String colorName = data.getColorName();
                text_voersearitem_spec.setText(colorName);

                String recommend = data.getSuperRecommend();
                text_oversearitem_recommendcontent.setText(recommend);

                List<Oversear_item_Bean.DataBean.SpecificationsBean> specifications = data.getSpecifications();
                  if(specifications!=null&&specifications.size()>0) {
                      for (int i = 0; i <specifications.size() ; i++) {
                           if(i==0) {
                               Oversear_item_Bean.DataBean.SpecificationsBean bean = specifications.get(0);
                               String colorName1 = bean.getColorName();
                               String price1 = bean.getPrice();
                               oversear_itemspecexplain1.setText(colorName1);
                               oversear_itemprice1.setText(price1);
                           }else if(i==1) {
                               Oversear_item_Bean.DataBean.SpecificationsBean bean = specifications.get(1);
                               String colorName1 = bean.getColorName();
                               String price1 = bean.getPrice();
                               oversear_itemspecexplain2.setText(colorName1);
                               oversear_itemprice2.setText(price1);
                           }else if(i==2) {
                               Oversear_item_Bean.DataBean.SpecificationsBean bean = specifications.get(2);
                               String colorName1 = bean.getColorName();
                               String price1 = bean.getPrice();
                               oversear_itemspecexplain3.setText(colorName1);
                               oversear_itemprice3.setText(price1);
                           }else if(i==3) {
                               Oversear_item_Bean.DataBean.SpecificationsBean bean = specifications.get(3);
                               String colorName1 = bean.getColorName();
                               String price1 = bean.getPrice();
                               oversear_itemspecexplain4.setText(colorName1);
                               oversear_itemprice4.setText(price1);
                           }else if(i==4) {
                               Oversear_item_Bean.DataBean.SpecificationsBean bean = specifications.get(4);
                               String colorName1 = bean.getColorName();
                               String price1 = bean.getPrice();
                               oversear_itemspecexplain5.setText(colorName1);
                               oversear_itemprice5.setText(price1);
                           }else if(i==5) {
                               Oversear_item_Bean.DataBean.SpecificationsBean bean = specifications.get(5);
                               String colorName1 = bean.getColorName();
                               String price1 = bean.getPrice();
                               oversear_itemspecexplain6.setText(colorName1);
                               oversear_itemprice6.setText(price1);
                           }
                      }
                  }
                List<Oversear_item_Bean.DataBean.GoodsImagesDosBean> dosBeen = data.getGoodsImagesDos();
                if(dosBeen!=null&&dosBeen.size()>0) {
                    Oversear_item_Bean.DataBean.GoodsImagesDosBean bean = dosBeen.get(0);
                    String imageMd5 = bean.getImageMd5();
                     application.setImagepath1(imageMd5);
                    Picasso.with(getApplicationContext()).load(imageMd5).into(image_voersearitem_shoppicture1);

                    Oversear_item_Bean.DataBean.GoodsImagesDosBean bean1 = dosBeen.get(1);
                    String imageMd51 = bean1.getImageMd5();
                    application.setImagepath2(imageMd51);
                    Picasso.with(getApplicationContext()).load(imageMd51).into(image_voersearitem_shoppicture2);

                    Oversear_item_Bean.DataBean.GoodsImagesDosBean bean2 = dosBeen.get(2);
                    String imageMd52 = bean2.getImageMd5();
                      application.setImagepath3(imageMd52);
                    Picasso.with(getApplicationContext()).load(imageMd52).into(image_voersearitem_shoppicture3);

                    Oversear_item_Bean.DataBean.GoodsImagesDosBean bean3 = dosBeen.get(3);
                    String imageMd53 = bean3.getImageMd5();
                    application.setImagepath4(imageMd53);
                    Picasso.with(getApplicationContext()).load(imageMd53).into(image_voersearitem_shoppicture4);


                    Oversear_item_Bean.DataBean.GoodsImagesDosBean bean4 = dosBeen.get(4);
                    String imageMd54 = bean4.getImageMd5();
                    application.setImagepath5(imageMd54);
                    Picasso.with(getApplicationContext()).load(imageMd54).into(image_voersearitem_shoppicture5);

                    String banner = data.getBaoBanner();
                    Picasso.with(getApplicationContext()).load(banner).into(image_oversearitem_ensure);
                }
            }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
           Toast.makeText(Mainpage_oversear_shopActivity.this, "网络出错,请检查网络", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initView() {
       oversear_item_back= (ImageView) findViewById(R.id.oversear_item_back);
        oversear_item_share= (ImageView) findViewById(R.id.oversear_item_share);
        image_oversearitem_picture= (ImageView) findViewById(R.id.image_oversearitem_picture);
        text_voersearitem_sendtime= (TextView) findViewById(R.id.text_voersearitem_sendtime);
        text_oversearitem_explain= (TextView) findViewById(R.id.text_oversearitem_explain);
        text_oversearitem_price= (TextView) findViewById(R.id.text_oversearitem_price);
        text_oversearitem_oldprice= (TextView) findViewById(R.id.text_oversearitem_oldprice);
        text_voersearitem_spec= (TextView) findViewById(R.id.text_voersearitem_spec);
        text_oversearitem_recommendcontent= (TextView) findViewById(R.id.text_oversearitem_recommendcontent);
        image_voersearitem_shoppicture1= (ImageView) findViewById(R.id.image_voersearitem_shoppicture1);
        image_voersearitem_shoppicture2= (ImageView) findViewById(R.id.image_voersearitem_shoppicture2);
        image_voersearitem_shoppicture3= (ImageView) findViewById(R.id.image_voersearitem_shoppicture3);
        image_voersearitem_shoppicture4= (ImageView) findViewById(R.id.image_voersearitem_shoppicture4);
        image_voersearitem_shoppicture5= (ImageView) findViewById(R.id.image_voersearitem_shoppicture5);
        image_oversearitem_ensure= (ImageView) findViewById(R.id.image_oversearitem_ensure);
        image_voersearitem_arrow= (ImageView) findViewById(R.id.image_voersearitem_arrow);


        view = LayoutInflater.from(this).inflate(R.layout.oversear_itemspec, null);
        oversear_itemspecexplain1= (TextView) view.findViewById(R.id.oversear_itemspecexplain1);
        oversear_itemprice1= (TextView) view.findViewById(R.id.oversear_itemprice1);
        oversear_itemspecexplain2= (TextView) view.findViewById(R.id.oversear_itemspecexplain2);
        oversear_itemprice2= (TextView) view.findViewById(R.id.oversear_itemprice2);
        oversear_itemspecexplain3= (TextView) view.findViewById(R.id.oversear_itemspecexplain3);
        oversear_itemprice3= (TextView) view.findViewById(R.id.oversear_itemprice3);
        oversear_itemspecexplain4= (TextView) view.findViewById(R.id.oversear_itemspecexplain4);
        oversear_itemprice4= (TextView) view.findViewById(R.id.oversear_itemprice4);
        oversear_itemspecexplain5= (TextView) view.findViewById(R.id.oversear_itemspecexplain5);
        oversear_itemprice5= (TextView) view.findViewById(R.id.oversear_itemprice5);
        oversear_itemspecexplain6= (TextView) view.findViewById(R.id.oversear_itemspecexplain6);
        oversear_itemprice6= (TextView) view.findViewById(R.id.oversear_itemprice6);

        image_oversearitem_eavluatedetails= (ImageView) findViewById(R.id.image_oversearitem_eavluatedetails);
        image_oversearitem_shopdetails= (ImageView) findViewById(R.id.image_oversearitem_shopdetails);
        image_oversearitem_specnumber= (ImageView) findViewById(R.id.image_oversearitem_specnumber);
    }
}
